package com.mes.web.controller.tdd;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mes.entity.tdd.ProcurementChukuMateriel;
import com.mes.entity.tdd.tj.Tpage;
import com.mes.service.tdd.ProcurementChukuMaterielService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;
/**
 * 
 *@ClassName: ProcurementChukuMaterielController 
 *@Description: 退货单详情
 *@Author tdd
 *@Date 2018-6-5 下午3:45:32 
 *@version 1.0
 */
@Controller
@RequestMapping("/procurementChukuMateriel")
public class ProcurementChukuMaterielController {
	//注入退货单详情Server
	@Resource public ProcurementChukuMaterielService chukuMaterielService;
	/**
	 * 退货单详情分页
	 * @param pageBean 条件+分页
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/list")
	public void materieDemandList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		//未删除
		pageBean.setIsdelete("0");
		List<ProcurementChukuMateriel> list = chukuMaterielService.list(pageBean);
		int total=chukuMaterielService.total(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 添加
	 * @param response response
	 * @param chukuMateriel 退货单详情
	 * @param request request对象
	 * @throws IOException IO流异常
	 */
	@RequestMapping("/add")
	public void add(HttpServletResponse response,ProcurementChukuMateriel chukuMateriel,HttpServletRequest request) throws IOException{
    	//添加
    	Long id = chukuMaterielService.add(chukuMateriel);
    	//添加成功
    	if (null != id && id >= 1) {
    		response.getWriter().print("1");
    		//未成功
    	} else {
    		response.getWriter().print("0");
    	}
	}
	/**
	 * 修改
	 * @param chukuMateriel 退货单详情
	 * @param response response对象
	 * @param request request对象
	 * @throws IOException IO流异常
	 */
	@RequestMapping("/update")
	public void updata(ProcurementChukuMateriel chukuMateriel,HttpServletResponse response,HttpServletRequest request) throws IOException{
		//根据id查数据
        //修改前数据
    	Tpage pageBean = new Tpage();
		//获取id
		pageBean.setPcmid(chukuMateriel.getPcmid());
		//页数
		pageBean.setPage(0);
		//每页条数
		pageBean.setPageSize(10);
		List<ProcurementChukuMateriel> list = chukuMaterielService.list(pageBean);
		//如果有该条数据
		if (list.size()>0) {
			ProcurementChukuMateriel cm2 = list.get(0);
			if( null!= chukuMateriel.getTuimoney() && !chukuMateriel.getTuimoney().equals("")){
				cm2.setTuimoney(chukuMateriel.getTuimoney());
			}
			if( null!= chukuMateriel.getNumber() && !chukuMateriel.getNumber().equals("")){
				cm2.setNumber(chukuMateriel.getNumber());
			}
			if( null!= chukuMateriel.getTuireason() && !chukuMateriel.getTuireason().equals("")){
				cm2.setTuireason(chukuMateriel.getTuireason());
			}
        	//修改
			int n = chukuMaterielService.updata(cm2);
			//修改成功
			if (n >= 1) {
				response.getWriter().print("1");
				//未成功
			} else {
				response.getWriter().print("0");
			}
		//如果没有该条数据(没有)
		}else {
			//该数据不存在
			response.getWriter().print("2");
		}
	}
	/**
	 * 删除
	 * @param ids 退货单详情id以","拼接而成的字符串
	 * @param request request对象
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletRequest request,HttpServletResponse response) throws Exception{
        List<Long> listId = StringUtil.splitListL(ids, ",");
        for (int i = 0; i < listId.size(); i++) {
        	//根据id查数据
        	//修改前数据
        	Tpage pageBean = new Tpage();
        	//获取id
        	pageBean.setPcmid(listId.get(i));
        	//页数
    		pageBean.setPage(0);
    		//每页条数
    		pageBean.setPageSize(10);
        	List<ProcurementChukuMateriel> list = chukuMaterielService.list(pageBean);
        	//如果有该条数据
        	if (list.size()>0) {
        		ProcurementChukuMateriel chukuMateriel = list.get(0);
        		//是否删除指定删除
        		//删除（假删除）
        		int n = chukuMaterielService.updatadelete(chukuMateriel);
        		//删除成功
        		if (n >= 1) {
        			if (i == (listId.size()-1)) {
        				response.getWriter().print("1");
					}
        			//未成功
        		} else {
        			response.getWriter().print("0");
        			continue;
        		}
        		//如果没有该条数据(没有)
        	}else {
        		//该数据不存在
        		response.getWriter().print("2");
        		continue;
        	}
		}
	}	
		
}
